package com.health.mapp.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

public class GenSerialNum {

	private static String databaseCount="ABCDEFGH";
	private static String nodeId="999";
	private static String count = "00000000";
	private static String dateValue = "20160111";
	/**
	 * 产生流水号
	 */
	public synchronized static long getMoveOrderNo() {
		long No = 0;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String nowdate = sdf.format(new Date());
		No = Long.parseLong(nowdate);
		//每天都要从00000000开始
		if (!(String.valueOf(No)).equals(dateValue)) {
			 if("ABCDEFGH".equals(databaseCount))
			 {
				 count = "00000000";
				 
			 }
			 else
			 {
				 count = databaseCount; 
			 }
			
			dateValue = String.valueOf(No);
		}
		String num = String.valueOf(No);
		num += getNo(count);
		// num = "CB" + num;
		return Long.valueOf(num);
	}

	/**
	 * 获取撤展单序列号
	 */
	public synchronized static long getMoveOrderNo(String serialNum) {
		String nyr = serialNum.substring(2, 10); // 获取年月日字符串
		String countV = serialNum.substring(10); // 获取流水号
		if (Integer.valueOf(countV) > Integer.valueOf(count)) {
			dateValue = nyr;
			count = String.valueOf(countV);
		}
		return getMoveOrderNo();
	}

	/**
	 * 返回当天的订单数+1
	 */
	public static String getNo(String s) {
		StringBuffer rs = new StringBuffer();
		int i = Integer.parseInt(s);
		i += 1;
		rs =  rs.append(i) ;
		for (int j = rs.length(); j < 8; j++) {
			//rs = "0" + rs;
			rs.insert(0, "0");
		}
		count = rs.toString();
		return rs.insert(0, nodeId).toString();
	}

	
	
	
	
	
 

	public static String getNodeId() {
		return nodeId;
	}

	public static void setNodeId(String nodeId) {
		GenSerialNum.nodeId = nodeId;
	}

	public static String getDatabaseCount() {
		return databaseCount;
	}

	public static void setDatabaseCount(String databaseCount) {
		GenSerialNum.databaseCount = databaseCount;
	}

	public static void main(String[] args) {
		for (int i = 0; i < 10; i++) {
			System.out.println(getMoveOrderNo());
		}
	}

}
